package com.sjtuy.galtxtreader;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.documentfile.provider.DocumentFile;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.Toast;

import java.io.File;
import java.io.FileInputStream;

public class MainActivity extends Activity {
    ListView mainListLeft;
    BookListAdapter bookListAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mainListLeft=findViewById(R.id.mainListLeft);
        bookListAdapter = new BookListAdapter(mainListLeft);
        mainListLeft.setAdapter(bookListAdapter);
    }

    final int REQUEST_BOOKTXT=1001;
    public void importBook(View view){
            Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
            intent.setType("text/plain");
            startActivityForResult(intent, REQUEST_BOOKTXT);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == REQUEST_BOOKTXT) {
                Uri bookFileUri = data.getData();
                bookListAdapter.addBook(bookFileUri);
            }
        }
    }
}
